import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '@/views/main.vue'
import store from '@/store/index.js'

Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Main',
      component: Main,
      redirect: 'login',
      children: [
        {
          path: 'home',
          component: () => import('@/views/home.vue'),
          name: 'home',
        },
        {
          path: 'mallStatus',
          component: () => import('@/views/mallStatus.vue'),
          name: 'mallStatus',
        },
        {
          path: 'mallInfo',
          component: () => import('@/views/mallInfo.vue'),
          name: 'mallInfo',
        },
        
        {
          path: 'user',
          component: () => import('@/views/user.vue'),
          name: 'user',
        },
        
        {
          path: 'baseInfo',
          component: () => import('@/views/person/baseInfo.vue'),
          name: 'baseInfo',
        },
        {
          path: 'refreshPwd',
          component: () => import('@/views/person/refreshPwd.vue'),
          name: 'refreshPwd',
        },
      ],
    },
    {
      path: '/login',
      component:()=> import('@/views/login.vue'),
      name: 'login',
    }
  ]
})
router.beforeEach((to, from, next) => {
  store.commit('getToken')
  const token = store.state.token
  // 如果token为空或者to.name不等于login
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
    console.log(to.name)
  } else {
    next()
  }
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
export default router
